from selenium import webdriver
from lxml import etree
import requests
import time

# 爬取部分
def main(word):
    # 无界面模式
    options = webdriver.ChromeOptions()
    # #     无界面启动，也可以直接设置options.headless=True
    options.add_argument('headless')
    driver = webdriver.Chrome(options = options)
    driver.implicitly_wait(10)  # 设置隐式等待
    url = 'https://image.baidu.com/search/index?tn=baiduimage&word=' + word
    driver.get(url)
    driver.enconding = 'UTF-8'
    # 获取页面初始高度
    js = "return action=document.body.scrollHeight"
    height = driver.execute_script(js)
    # 将滚动条调整至页面底部
    driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
    time.sleep(3)
    # 定义初始时间戳（秒）
    t1 = int(time.time())
    # 重试次数
    # 定义循环标识，用于终止while循环
    status = True
    # 重试次数
    num = 0
    while status:
        # 获取当前时间戳（秒）
        t2 = int(time.time())
        # 判断时间初始时间戳和当前时间戳相差是否大于30秒，小于30秒则下拉滚动条
        if t2 - t1 < 30:
            new_height = driver.execute_script(js)
            if new_height > height:
                time.sleep(2)
                driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
                # 重置初始页面高度
                height = new_height
                # 重置初始时间戳，重新计时
                t1 = int(time.time())
        elif num < 3:  # 当超过30秒页面高度仍然没有更新时，进入重试逻辑，重试3次，每次等待30秒
            time.sleep(3)
            num = num + 1
        else:  # 超时并超过重试次数，程序结束跳出循环，并认为页面已经加载完毕！
            print("滚动条已经处于页面最下方！")
            status = False
            # 滚动条调整至页面顶部
            driver.execute_script('window.scrollTo(0, 0)')
            break
        content = driver.page_source.encode('utf-8')
        # 解析网页数据
        source = etree.HTML(content)
        # 获取图片链接
        pic_img = source.xpath('//div[@class="imgbox-border"]/a/img/@data-imgurl')
        header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36'}
        for i in range(len(pic_img)):
            pic = requests.get(pic_img[i], headers=header)
            # 获取图片保存的路径
            path ='./img'
            base_url = path + "\image{}.jpg".format((i + 1))
            with open(base_url, 'wb') as f:
                f.write(pic.content)


if __name__=='__main__':
    start = time.time()
    main('泰勒斯威夫特')
    end = time.time()
    print('下载图片需要%s秒' % (end - start))